package tokens;
/**
 * A token that holds operators.
 * 
 * @author James Santos-Calacat
 * @version 26 February 2010
 *
 */
public class OperatorToken extends Token
{
	public static final char Plus = '+';
	public static final char Minus = '-';
	public static final char Mult = '*';
	public static final char Div = '/';
	public static final char LeftParen = '(';
	
	/**
	 * Symbolizes the power function.
	 */
	public static final char Caret = '^';
	
	/**
	 * The operator token.
	 */
	private char my_operator_token;
	
	/**
	 * Constructs an operator token.
	 * @param the_operator The operator.
	 */
	public OperatorToken (final char the_operator)
	{
		// add exceptions!!
		my_operator_token = the_operator;
	}
	
	/**
	 * Returns the operator token.
	 * @return What is your operator token?
	 */
	public char getOperatorToken()
	{
		return my_operator_token;
	}
	
	/**
	 * Return true if the char ch is an operator of a formula.
	 * Current operators are: +, -, *, /, (.
	 * @param ch  a char
	 * @return  whether ch is an operator
	 */
	public static boolean isOperator (char ch) {
	    return ((ch == Plus) ||
	            (ch == Minus) ||
	            (ch == Mult) ||
	            (ch == Div) ||
	            (ch == LeftParen) ||
	            (ch == Caret));
	}
	
	/**
	 * Given an operator, return its priority.
	 *
	 * priorities:
	 *   +, - : 0
	 *   *, / : 1
	 *   ^    : 2
	 *   (    : 3
	 *
	 * @param ch  a char
	 * @return  the priority of the operator
	 */
	 public static int operatorPriority (char ch) {
	    if (!isOperator(ch)) {
	        // This case should NEVER happen
	        System.out.println("Error in operatorPriority.");
	        System.exit(0);
	    }
	    switch (ch) {
	        case Plus:
	            return 0;
	        case Minus:
	            return 0;
	        case Mult:
	            return 1;
	        case Div:
	            return 1;
	        case Caret:
	        	return 2;
	        case LeftParen:
	            return 3;

	        default:
	            // This case should NEVER happen
	            System.out.println("Error in operatorPriority.");
	            System.exit(0);
	            break;
	    }
		return -1; // Should not go here!
	}
	
	/**
	 * Return the priority of this OperatorToken.
	 *
	 * priorities:
	 *   +, - : 0
	 *   *, / : 1
	 *   ^    : 2
	 *   (    : 3
	 *
	 * @return  the priority of operatorToken
	 */
	 public int priority () 
	 {
	    switch (this.my_operator_token) 
	    {
	        case Plus:
	            return 0;
	        case Minus:
	            return 0;
	        case Mult:
	            return 1;
	        case Div:
	            return 1;
	        case Caret:
	        	return 2;
	        case LeftParen:
	            return 3;

	        default:
	            // This case should NEVER happen
	            System.out.println("Error in priority.");
	            System.exit(0);
	            break;
	    }
	    
	    return -1; // should not go here!
	 }
}
